package com.bibrApp.difu.actvity.publist_jieru_mvp;

import android.content.Context;
import android.util.Log;

import com.bibrApp.difu.Response.FBInfoResBody;
import com.bibrApp.difu.Response.NoticeResBody;
import com.bibrApp.difu.Response.PaySettingResPonseBody;
import com.bibrApp.difu.Response.TradePawnInfo;
import com.bibrApp.difu.actvity.others.IOtherView;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class PublistJieruPresenter extends BasePresenter<IPublistJieruView> {
    public PublistJieruPresenter(Context context, IPublistJieruView iView) {
        super(context, iView);
    }

    /**
     * 获取法币币种信息
     */
    public void getBInfo() {
        RetrofitNet.getInstance()
                .getApi()
                .getBInfo()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            FBInfoResBody fbInfoResBody = new Gson().fromJson(jsonStr,FBInfoResBody.class);
                            if(fbInfoResBody == null)
                                return;
                            iView.successOfGetFBInfo(fbInfoResBody);
                        }

                    }
                });

    }
    /**
     * 获取抵押币币种信息
     */
    public void getTradeBInfo() {
        RetrofitNet.getInstance()
                .getApi()
                .getTradePawnInfo()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            List<TradePawnInfo> list = new Gson().fromJson(jsonStr,new TypeToken<List< TradePawnInfo >>(){}.getType());
                            if(list == null)
                                return;
                            iView.successOfTradeBInfo(list);
                        }

                    }
                });

    }

    public void getBNewPrice(String bName) {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("coinEunit", bName);
        RetrofitNet.getInstance()
                .getApi()
                .getBNewPrice(bName)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            iView.successOfGetBNewPrice(String.valueOf(value));
                        }

                    }
                });

    }
    /**
     * 发布借入
     legalCoinId	String	是	法币币种ID
     pawnCoinId	String	是	抵押币币种ID
     legalQuantity	String	是	法币数量ID
     cycle	int	是	周期
     pawnRate	Bigdecimal	是	抵押率
     legalDailyRate	Bigdecimal	是	日利率
     totalInterest	Bigdecimal	是	总利息
     lowPrice	Bigdecimal	是	最低价
     highPrice	Bigdecimal	是	最高价
     tradePassword	String	是	交易密码
     */
    public void publishAd(String legalCoinId ,String pawnCoinId ,String legalQuantity, int cycle, String pawnRate,
                        String legalDailyRate ,String totalInterest , String lowPrice,String highPrice ,String tradePassword) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("legalCoinId", legalCoinId);
        map.put("pawnCoinId", pawnCoinId);
        map.put("legalQuantity", legalQuantity);
        map.put("cycle", cycle);
        map.put("pawnRate", pawnRate);
        map.put("legalDailyRate", legalDailyRate);
        map.put("totalInterest", totalInterest);
        map.put("lowPrice", lowPrice);
        map.put("highPrice", highPrice);
        map.put("tradePassword", tradePassword);
        RetrofitNet.getInstance()
                .getApi()
                .publishJieRu(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        iView.success("发布借入成功");
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        Log.i("ttt", "--------" + msg);
                    }
                });
    }

}
